package com.example.txl.activity;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.txl.MainActivity;
import com.example.txl.R;
import com.example.txl.bean.PeoBean;
import com.example.txl.dao.PeoDao;

public class DetailsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // 获取id
        String id = getIntent().getStringExtra("id");
        // 获取电话号
        TextView num = findViewById(R.id.details_phone);
        // 打电话
        Button call_phone = findViewById(R.id.details_da_phone);
        call_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 判断是否有权限
                int a = ContextCompat.checkSelfPermission(DetailsActivity.this, Manifest.permission.CALL_PHONE);// 获取电话权限的ID
                if(a== PackageManager.PERMISSION_GRANTED){
                    // 有权限
                    Log.d("打电话","有权限");
                    makePhoneCall(num.getText().toString().trim());
                }else {
                    Log.d("打电话","无权限");
                    ActivityCompat.requestPermissions(DetailsActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                }
            }
        });
        // 发短信
        Button details_da_message = findViewById(R.id.details_da_message);
        details_da_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 创建一个隐式意图，指定作为打开短信应用
                Intent ms_intent = new Intent(Intent.ACTION_SENDTO);
                // 设置URI，指定短信的“smsto”：协议，将打开默认短信应用程序
                Uri uri = Uri.parse("smsto:" + Uri.encode(num.getText().toString().trim()));
                ms_intent.setData(uri);
//                ms_intent.putExtra("sms_body", "");
                startActivity(ms_intent);
            }
        });

        // 返回功能
        Button back = findViewById(R.id.details_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent back_intent = new Intent(DetailsActivity.this, MainActivity.class);
                startActivity(back_intent);
            }
        });

        // 删除功能
        Button del = findViewById(R.id.details_del);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PeoDao.delPeo(id);
                //显示一个短暂的提示消息，表示删除成功
                Toast.makeText(DetailsActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                Intent del_intent = new Intent(DetailsActivity.this, MainActivity.class);
                startActivity(del_intent);
            }
        });

        // 点击更改
        Button details_update = findViewById(R.id.details_update);
        details_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent bj_intent = new Intent(DetailsActivity.this, UpdateActivity.class);
                //id作为额外的数据传递给UpdateActivity，根据这个id来获取要更新的人员信息
                bj_intent.putExtra("id", id);
                startActivity(bj_intent);
            }
        });

        // 详情信息
        PeoBean peo = PeoDao.getOnePeo(id);
        // 根据性别设置头像
        ImageView tx = findViewById(R.id.details_tx);
        if (peo.getSex().equals("男")){
            tx.setImageResource(R.drawable.peo_man);
        }else {
            tx.setImageResource(R.drawable.peo_woman);
        }
        // 姓名
        TextView name = findViewById(R.id.details_name);
        name.setText(peo.getName());
        // 电话
        TextView phone = findViewById(R.id.details_phone);
        phone.setText(peo.getNum());
        // 性别
        TextView sex = findViewById(R.id.details_sex);
        sex.setText("性别：" + peo.getSex());
        // 备注
        TextView remark = findViewById(R.id.details_bz);
        remark.setText(peo.getRemark());


    }
    // 打电话功能
    private void makePhoneCall(String num){
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        // : 不能漏   设置要拨打的电话号码（将电话号码包装成Uri格式）
        callIntent.setData(Uri.parse("tel:" + num));
        startActivity(callIntent);
    }
}
